import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flutter_flame/routes/RouteGame.dart';
import '../config/GameConfig.dart';

// 实体类 - Sprite
abstract class SpriteEntity extends SpriteComponent
    with
        HasGameReference<RouterGame>,
        CollisionCallbacks,
        TapCallbacks,
        GameConfig {
  String spriteImg;
  double scaleHitBoxK = .8; // 碰撞矩形放缩

  SpriteEntity(this.spriteImg);

  @override
  Future<void> onLoad() async {
    sprite = await Sprite.load(spriteImg);
  }

  // 添加碰撞矩阵
  void addHitBox() {
    add(
      RectangleHitbox(
        size: size * scaleHitBoxK,
        position: Vector2(
          (size * (1 - scaleHitBoxK)).x / 2,
          (size * (1 - scaleHitBoxK)).y / 2,
        ),
      ),
    );
  }

  // 设置大小（等比例）
  void setSize(double w) {
    final radio = sprite!.image.width / sprite!.image.height;
    size = Vector2(w, w / radio);
  }

  // 改变图案
  Future<void> changeSprite(String newSpriteImage) async {
    sprite = await Sprite.load(newSpriteImage);
  }
}
